package config

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var DB *gorm.DB

func MustInitMysql() {
	if Data.DB.Type != "mysql" {
		panic("请设置DB type=mysql")
	}
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		Data.DB.UserName, Data.DB.Password, Data.DB.Host, Data.DB.Port, Data.DB.DBName)
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(fmt.Sprintf("failed to connect database:%s", err))
	}

	DB = db
}
